import 'package:flutter/material.dart';
import 'package:shop/route/my_route.dart';
import 'package:shared_preferences/shared_preferences.dart';

class Config{
  Config._();

  // "http://103.240.151.243:8081"
  // http://thb.asiaunicom.com
  // String host = "http://thb.asiaunicom.com" ;
  // String xlhost = "ws://192.168.0.15:3000" ;
  // String host = "http://xllm.szghrj.com:8082";
  // String host = "http://10.0.2.2:8082";
  // String xlhost = "http://xllm.szghrj.com:8082";
  // String host = "http://xllm.szghrj.com:8082";
  // String xlhost = "http://xllm.szghrj.com:8082";
  // String host = "http://192.168.0.240:8082";
  String host = "http://10.0.2.2:8082";//本地
  String xlhost = "http://10.0.2.2:8082";
  String username ;
  String token ;
  String icon ;
  String bh ;
  String dy_host ;
  String zhibo_url ;
  bool control = false ;



  SharedPreferences spf = null ;

  static Config _instance;

  void setToken(String token){
    this.spf.setString("token", token) ;
    this.token = token ;
  }

  void clear(){
    this.token = null ;
    this.username = null ;
    this.icon = null ;
    // this.bh = null ;
    spf.setString("token", "") ;
    spf.setString("username", "") ;
    spf.setString("icon", "") ;
    // spf.setString("bh", "") ;
  }

  void init(SharedPreferences spf){
    this.spf = spf ;
    this.token = spf.getString("token") ;
    this.username = spf.getString("username") ;
    this.bh = spf.getString("bh") ;
    this.icon = spf.getString("icon") ;
    this.dy_host = spf.getString("dy_host") ;
    this.zhibo_url = spf.getString("zhibo_url") ;
  }

  void setDyHost(String host){
    this.dy_host = host ;
    spf.setString("dy_host", host) ;
  }

  void setZhiboUrl(String url){
    this.zhibo_url = url ;
    spf.setString("zhibo_url", url) ;
  }

  String getZhiboUrl(){
    return this.zhibo_url ;
  }

  String getDyHost(){
    return this.dy_host ;
  }

  String getToken(){
    return this.token ;
  }

  void setUsername(String username){
    spf.setString("username", username) ;
    this.username = username ;
  }

  void setIcon(String icon) {
    spf.setString("icon", icon) ;
    this.icon = icon ;
  }

  void setBh(String bh) {
    spf.setString("bh", bh) ;
    this.bh = bh ;
  }

  String getHost(){
    if(!this.host.endsWith("/")) {
      host += "/" ;
    }
    return host ;
  }

  void logout(){
    this.clear() ;
    MyRoute.pushNamedAndRemoveUntil("/login" , (route) => route == null) ;
  }

  void setHost(String host){
    this.host = host ;
  }

  static Config getInstance() {
    if (_instance == null) {
      _instance = Config._();
    }
    return _instance;
  }

  String getImgUrl(img) {
    if(img == null) {
      return null ;
    }
    if(img.startsWith("/")) {
      img = img.substring(1) ;
    }
    return getHost() + img ;
  }

  String getDyImgUrl(img) {
    if(img == null) {
      return "" ;
    }
    if(img.startsWith("http")||img.startsWith("https")) {
      return img ;
    }
    if(img.startsWith("/")) {
      img = img.substring(1) ;
    }
    return getDyHost() + img ;
  }
}